package com.itheima.admin.controller;

import com.itheima.admin.dto.AdChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.controller
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "频道管理")
public class AdChannelController {

    @Resource
    private AdChannelService adChannelService;

    /**
     * 频道列表分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("频道列表分页查询")
    public PageResultVo list(@RequestBody AdChannelPageRequestDto dto){
        // 调用业务层
        PageResultVo vo = adChannelService.findPage(dto);
        // 响应结果
        return vo;
    }

    /**
     * 添加频道
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("添加频道")
    //@Validated|@Valid参数需要校验
    public ResultVo save(@RequestBody @Validated AdChannel adChannel){
        adChannelService.add(adChannel);
        return ResultVo.ok("操作成功");
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过id查询频道")
    public ResultVo findById(@PathVariable(value = "id") Integer id){
        AdChannel adChannel = adChannelService.getById(id);
        return ResultVo.ok(adChannel);
    }

    /**
     * 更新频道
     * @param adChannel
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新频道")
    public ResultVo update(@RequestBody AdChannel adChannel){
        adChannelService.updateById(adChannel);
        return ResultVo.ok("操作成功");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("通过id删除频道")
    public ResultVo deleteById(@PathVariable(value = "id") Integer id){
        adChannelService.removeById(id);
        return ResultVo.ok("操作成功");
    }
}
